<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nyext="urn:noury-extensions">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:param name="package" />
  <xsl:param name="outdir" />
  <xsl:param name="name" />
  <xsl:param name="domainName" />
  <xsl:variable name="domain" select="/manifest/types/domain[@name=$domainName]" />

  <xsl:include href="functions.xslt" />

  <xsl:template name="generate-model-file">
  <xsl:variable name="src">
  <xsl:text>// $Id$

using System;
using System.Collections.ObjectModel;
using System.Text;
using Salamanca;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;

namespace </xsl:text>
          <xsl:value-of select="$package"/>
          <xsl:text>
{



    partial class </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>CrudData 
    {

        partial struct Backup
        {
            public object </xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>;
        }

        private object _CreateBackup()
        {
            Backup ret=new Backup();
            if (_</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text> != null)
                ret.</xsl:text>
                <xsl:value-of select="$domainName"/>
                <xsl:text> = ((IBackupable)_</xsl:text>
                <xsl:value-of select="$domainName"/>
                <xsl:text>).CreateBackup();
            return ret;
        }

        private void _RestoreBackup(object backup)
        {
            if (((Backup)backup).</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text> != null)
                ((IBackupable)_</xsl:text>
                <xsl:value-of select="$domainName"/>
                <xsl:text>).RestoreBackup(((Backup)backup).</xsl:text>
                <xsl:value-of select="$domainName"/>
                <xsl:text>);
        }

        private DomainModel.</xsl:text>
        <xsl:value-of select="$domainName"/>
        <xsl:text> _</xsl:text>
        <xsl:value-of select="$domainName"/>
        <xsl:text>;
    }
}
    </xsl:text>
  </xsl:variable>
  <xsl:value-of select="nyext:generateFile($src, nyext:combinePath($outdir, concat(nyext:getModelFileName($name), '.cs')))" />
</xsl:template>

  <xsl:template name="generate-user-file">
    <xsl:text>// $Id: </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>Info.cs $

using System;
using System.Collections.ObjectModel;
using System.Text;
using Salamanca;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;

namespace </xsl:text>
    <xsl:value-of select="$package"/>
    <xsl:text>
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Holds data for a &lt;see cref="DomainModel.</xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>" />" /&gt; CRUD activity.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>CrudData:
        ActivityData,
        DomainModel.I</xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>Container
    {

        /// &lt;summary&gt;Backup structure.&lt;/summary&gt;
        internal partial struct Backup
        {
        }

        /// &lt;summary&gt;Creates an new instance of the &lt;see cref="</xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>CrudData" />" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>CrudData()
        {
        }

        /// &lt;summary&gt;Creates a backup of this data.&lt;/summary&gt;
        /// &lt;returns&gt;An object containing the backup data.&lt;/returns&gt;
        protected override object CreateBackup()
        {
            return _CreateBackup();
        }

        /// &lt;summary&gt;Restores a backup into this instance.&lt;/summary&gt;
        /// &lt;param name="backup"&gt;The object containing the backup data.&lt;/param&gt;
        protected override void RestoreBackup(object backup)
        {
            _RestoreBackup(backup);
        }

        /// &lt;summary&gt;Gets or sets the &lt;see cref="DomainModel.</xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>" />" /&gt;.&lt;/summary&gt;
        public DomainModel.</xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>
        {
            get { return _</xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>; }
            set { _</xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text> = value; }
        }

    }
}
    </xsl:text>
  </xsl:template>

<xsl:template match="/">
  <xsl:call-template name="generate-model-file" />
  <xsl:call-template name="generate-user-file" />
</xsl:template>
</xsl:stylesheet>
